const db = wx.cloud.database();
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    rili: [{
        title: '公告发布',
        ontap: 'ontap0',
        on: true,
        date: '10月14日'
      }, {
        title: '报名时间',
        ontap: 'ontap1',
        on: 'q1',
        date: '10月15日 08:00'
      },
      {
        title: '资格审查查询',
        ontap: 'ontap2',
        on: 'q2',
        date: '10月15--26日'
      },
      {
        title: '报名序号查询',
        ontap: 'ontap3',
        on: 'q3',
        date: '10月28日8:00后 '
      },

      {
        title: '报名确认缴费',
        ontap: 'ontap4',
        on: 'q4',
        date: '11月1日0:00'
      },
      {
        title: '准考证打印',
        ontap: 'ontap5',
        on: 'q5',
        date: '11月22日0:00',
      },
      {
        title: '笔试时间',
        ontap: 'ontap6',
        on: 'q6',
        date: '11月28日'
      },
      {
        title: '成绩查询',
        ontap: 'ontap7',
        on: 'q7',
        date: '2022年1月'
      }
    ],
    page: 118
  },
  getPhoneNumber(e) {
    var _ = this;

    wx.cloud.callFunction({
      name: 'getMobile',
      data: {
        weRunData: wx.cloud.CloudID(e.detail.cloudID),
      }
    }).then(res => {
      _.setData({
        mobile: res.result,
      })
      console.log(res.result)
      wx.request({
        url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/collect_v3',
        method: 'POST',
        data: {
          activeId: 36,
          mobile: res.result,
          areaCode: _.data.area,
          agent: _.data.geneal,
          pageId: _.data.page
        },
        success: res => {
          console.log(res);
        }
      })

      wx.setStorageSync('riliphone', _.data.mobile)
      wx.request({ //发送请求
        url: 'https://zg99.offcn.com/index/biaodan/register?actid=42651&callback=?', //接收url
        data: {
          phone: _.data.mobile,
          fenxiao: _.data.area,
          geneal: _.data.geneal,
        }, //接收参数
        success(res) {
          var jsonStr = res.data;
          jsonStr = jsonStr.replace("(", "");
          jsonStr = jsonStr.replace(")", "");
          if (typeof jsonStr != 'object') {
            jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
            var theJson = JSON.parse(jsonStr);
          }
          console.log(theJson)
          _.setData({
            login: 0
          })
        }
      });

    })

  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _ = this;
    console.log(options);
    _.setData({
      area: options.area,
      geneal: options.agent
    })
    if (options.page) {
      _.setData({
        page: options.page
      })
    }
    var q0 = wx.getStorageSync('q0')
    var q1 = wx.getStorageSync('q1')
    var q2 = wx.getStorageSync('q2')
    var q3 = wx.getStorageSync('q3')
    var q4 = wx.getStorageSync('q4')
    var q5 = wx.getStorageSync('q5')
    var q6 = wx.getStorageSync('q6')
    var q7 = wx.getStorageSync('q7')
    if (q0) {
      _.setData({
        'rili[0].on': false
      })
    }
    if (q1) {
      _.setData({
        'rili[1].on': false
      })
    }
    if (q2) {
      _.setData({
        'rili[2].on': false
      })
    }
    if (q3) {
      _.setData({
        'rili[3].on': false
      })
    }
    if (q4) {
      _.setData({
        'rili[4].on': false
      })
    }
    if (q5) {
      _.setData({
        'rili[5].on': false
      })
    }
    if (q6) {
      _.setData({
        'rili[6].on': false
      })
    }
    if (q7) {
      _.setData({
        'rili[7].on': false
      })
    }


  },
  ontap0: function () {
    var _ = this;
    var phone = wx.getStorageSync('riliphone')
    if (phone) {
      wx.showModal({
        title: '提示',
        content: '是否需要推送公告发布提醒',
        success(res) {
          if (res.confirm) {
            wx.setStorageSync('q0', '1')
            _.setData({
              'rili[0].on': false
            })
            console.log('用户点击确定')
            wx.requestSubscribeMessage({
              tmplIds: ['PkfFOPMIstm9lbzo0Ko18g3s5DZ_ZutVmWKXXrUnfQ8'], //笔试时间
              success(res) {
                console.log(res);
                db.collection('userInfo').where({
                  _openid: app.globalData.openid,
                }).get({
                  success: res => {
                    var _ = this;

                    db.collection('userInfo').add({
                      data: {
                        gonggaofabu: '1',
                        phone: phone
                      },
                      //添加成功的回调函数
                      success(res) {


                        wx.showToast({
                          title: '预约成功！',
                        })
                        console.log('cloud get success!')
                      }
                    });
                  },
                })
              },
              fail(res) {
                console.log(res);
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else {
      wx.showToast({
        title: '请先登录！',
        icon: 'none'
      })
      _.setData({
        login: 1
      })
    }
  },
  ontap1: function () {
    var _ = this;
    var phone = wx.getStorageSync('riliphone')
    if (phone) {
      wx.showModal({
        title: '提示',
        content: '是否需要推送报名时间提醒',
        success(res) {
          if (res.confirm) {
            wx.setStorageSync('q1', '1')
            _.setData({
              'rili[1].on': false
            })
            console.log('用户点击确定')
            wx.requestSubscribeMessage({
              tmplIds: ['5SgqaO3t1GPd4GTEvzOqIoJa92FlpuNeD1SnJb-fzpw'], //报名提醒
              success(res) {
                console.log(res);
                db.collection('userInfo').where({
                  _openid: app.globalData.openid,
                }).get({
                  success: res => {
                    var _ = this;
                    db.collection('userInfo').add({
                      data: {
                        baomingshijian: '1',
                        phone: phone
                      },
                      //添加成功的回调函数
                      success(res) {


                        wx.showToast({
                          title: '预约成功！',
                        })
                        console.log('cloud get success!')
                      }
                    });
                  },
                })
              },
              fail(res) {
                console.log(res);
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else {
      wx.showToast({
        title: '请先登录！',
        icon: 'none'
      })
      _.setData({
        login: 1
      })
    }

  },
  ontap2: function () {
    var _ = this;
    var phone = wx.getStorageSync('riliphone')
    if (phone) {
      wx.showModal({
        title: '提示',
        content: '是否需要推送资格审查查询提醒',
        success(res) {
          if (res.confirm) {
            wx.setStorageSync('q2', '1')
            _.setData({
              'rili[2].on': false
            })
            console.log('用户点击确定')
            wx.requestSubscribeMessage({
              tmplIds: ['e_c8eIZQU2lJvZzC64s9Uc6a5-4XOzN4vbQQnssRxA0'], //报名提醒
              success(res) {
                console.log(res);
                db.collection('userInfo').where({
                  _openid: app.globalData.openid,
                }).get({
                  success: res => {
                    var _ = this;
                    db.collection('userInfo').add({
                      data: {
                        zigeshencha: '1',
                        phone: phone
                      },
                      //添加成功的回调函数
                      success(res) {

                        wx.showToast({
                          title: '预约成功！',
                        })
                        console.log('cloud get success!')
                      }
                    });
                  },
                })
              },
              fail(res) {
                console.log(res);
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else {
      wx.showToast({
        title: '请先登录！',
        icon: 'none'
      })
      _.setData({
        login: 1
      })
    }
  },
  ontap3: function () {
    var _ = this;
    var phone = wx.getStorageSync('riliphone')
    if (phone) {
      wx.showModal({
        title: '提示',
        content: '是否需要推送报名序号查询提醒',
        success(res) {
          if (res.confirm) {
            wx.setStorageSync('q3', '1')
            _.setData({
              'rili[3].on': false
            })
            console.log('用户点击确定')
            wx.requestSubscribeMessage({
              tmplIds: ['e_c8eIZQU2lJvZzC64s9UWFmNAHSMD60TVNPug_LHGo'], //报名序号查询
              success(res) {
                console.log(res);
                db.collection('userInfo').where({
                  _openid: app.globalData.openid,
                }).get({
                  success: res => {
                    var _ = this;
                    db.collection('userInfo').add({
                      data: {
                        baomingxuhao: '1',
                        phone: phone
                      },
                      //添加成功的回调函数
                      success(res) {

                        wx.showToast({
                          title: '预约成功！',
                        })
                        console.log('cloud get success!')
                      }
                    });
                  },
                })
              },
              fail(res) {
                console.log(res);
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else {
      wx.showToast({
        title: '请先登录！',
        icon: 'none'
      })
      _.setData({
        login: 1
      })
    }
  },
  ontap4: function () {
    var _ = this;
    var phone = wx.getStorageSync('riliphone')
    if (phone) {
      wx.showModal({
        title: '提示',
        content: '是否需要推送报名确认缴费提醒',
        success(res) {
          if (res.confirm) {
            wx.setStorageSync('q4', '1')
            _.setData({
              'rili[4].on': false
            })
            console.log('用户点击确定')
            wx.requestSubscribeMessage({
              tmplIds: ['cupJGkKTq5TBvK_XViVgBsjzXDETsWzOVt0OQR4ztUg'], //报名确认缴费
              success(res) {
                console.log(res);
                db.collection('userInfo').where({
                  _openid: app.globalData.openid,
                }).get({
                  success: res => {
                    var _ = this;
                    db.collection('userInfo').add({
                      data: {
                        jiaofeitixing: '1',
                        phone: phone
                      },
                      //添加成功的回调函数
                      success(res) {

                        wx.showToast({
                          title: '预约成功！',
                        })
                        console.log('cloud get success!')
                      }
                    });
                  },
                })
              },
              fail(res) {
                console.log(res);
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else {
      wx.showToast({
        title: '请先登录！',
        icon: 'none'
      })
      _.setData({
        login: 1
      })
    }
  },
  ontap5: function () {
    var _ = this;
    var phone = wx.getStorageSync('riliphone')
    if (phone) {
      wx.showModal({
        title: '提示',
        content: '是否需要推送准考证打印提醒',
        success(res) {
          if (res.confirm) {
            wx.setStorageSync('q5', '1')
            _.setData({
              'rili[5].on': false
            })
            console.log('用户点击确定')
            wx.requestSubscribeMessage({
              tmplIds: ['xTZt7klyMYF30sCqFp_oaA141Dy-sg5UD-RcmkMAo1w'], //准考证打印
              success(res) {
                console.log(res);
                db.collection('userInfo').where({
                  _openid: app.globalData.openid,
                }).get({
                  success: res => {
                    var _ = this;
                    db.collection('userInfo').add({
                      data: {
                        zkz: '1',
                        phone: phone
                      },
                      //添加成功的回调函数
                      success(res) {

                        wx.showToast({
                          title: '预约成功！',
                        })
                        console.log('cloud get success!')
                      }
                    });
                  },
                })
              },
              fail(res) {
                console.log(res);
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else {
      wx.showToast({
        title: '请先登录！',
        icon: 'none'
      })
      _.setData({
        login: 1
      })
    }
  },
  ontap6: function () {
    var _ = this;
    var phone = wx.getStorageSync('riliphone')
    if (phone) {
      wx.showModal({
        title: '提示',
        content: '是否需要推送笔试时间提醒',
        success(res) {
          if (res.confirm) {
            wx.setStorageSync('q6', '1')
            _.setData({
              'rili[6].on': false
            })
            console.log('用户点击确定')
            wx.requestSubscribeMessage({
              tmplIds: ['wKIE9EocHpzUOcNDyvK1ohOW1aXKZu3t5D9dwVhtG9A'], //笔试时间
              success(res) {
                console.log(res);
                db.collection('userInfo').where({
                  _openid: app.globalData.openid,
                }).get({
                  success: res => {
                    var _ = this;
                    db.collection('userInfo').add({
                      data: {
                        bishishijian: '1',
                        phone: phone
                      },
                      //添加成功的回调函数
                      success(res) {

                        wx.showToast({
                          title: '预约成功！',
                        })
                        console.log('cloud get success!')
                      }
                    });
                  },
                })
              },
              fail(res) {
                console.log(res);
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else {
      wx.showToast({
        title: '请先登录！',
        icon: 'none'
      })
      _.setData({
        login: 1
      })
    }
  },
  ontap7: function () {
    var _ = this;
    var phone = wx.getStorageSync('riliphone')
    if (phone) {
      wx.showModal({
        title: '提示',
        content: '是否需要推送成绩查询提醒',
        success(res) {
          if (res.confirm) {
            wx.setStorageSync('q7', '1')
            _.setData({
              'rili[7].on': false
            })
            console.log('用户点击确定')
            wx.requestSubscribeMessage({
              tmplIds: ['sPoRMEuCnGBJqI74XEJPl1vWvvzXy7jKScbsY7J_iwc'], //准考证打印
              success(res) {
                console.log(res);
                db.collection('userInfo').where({
                  _openid: app.globalData.openid,
                }).get({
                  success: res => {
                    var _ = this;
                    db.collection('userInfo').add({
                      data: {
                        chengjicx: '1',
                        phone: phone
                      },
                      //添加成功的回调函数
                      success(res) {

                        wx.showToast({
                          title: '预约成功！',
                        })
                        console.log('cloud get success!')
                      }
                    });
                  },
                })
              },
              fail(res) {
                console.log(res);
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else {
      wx.showToast({
        title: '请先登录！',
        icon: 'none'
      })
      _.setData({
        login: 1
      })
    }
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})